package com.easyboot.web.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.easyboot.framework.core.ActionResult;
import com.easyboot.framework.core.BaseController;
import com.easyboot.framework.core.RequestHelper;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.web.system.service.SysLoginLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/6/25 16:16
 **/
@Controller
@RequestMapping("/admin/loginlog")
public class SysLoginLogController extends BaseController {


    @Autowired
    private RequestHelper requestHelper;
    @Autowired
    private SysLoginLogService loginLogService;


    /**
    *
     * @author yushuo
     * @description 登录日志页面
     * @date 16:17 2021/6/25
     * @param []
     * @return com.easyboot.framework.core.ActionResult
    **/
    @RequiresPermissions("admin:loginlog:view")
    @GetMapping("/index")
    public ActionResult index(ModelMap model){

        String startDate=DateUtil.format(DateUtil.offsetMonth(new Date(),-1),"yyyy-MM-dd");
        String endDate=DateUtil.format(new Date(),"yyyy-MM-dd");
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        return view();
    }

    /**
     * @author yushuo
     * @description 日志列表
     * @date 16:18 2021/6/25
     * @param [dto]
     * @return com.easyboot.framework.core.ActionResult
    **/
    @RequiresPermissions("admin:loginlog:view")
    @PostMapping("/getPageList")
    public ActionResult getPageList(DataTableDto dto){
        int loginStatus = requestHelper.getInt("loginStatus", -1);
        String loginName = requestHelper.getString("loginName","");
        String loginIP = requestHelper.getString("loginIP","");
        String startDate = requestHelper.getString("startDate","");
        String endDate = requestHelper.getString("endDate","");
        DataTableDto data= loginLogService.getPageList(dto,loginName,loginStatus,loginIP, DateUtil.parse(startDate,"yyyy-MM-dd"),DateUtil.parse(endDate,"yyyy-MM-dd"));
        return json(data);
    }

    /**
     * @author yushuo
     * @description 日志列表
     * @date 16:18 2021/6/25
     * @param [dto]
     * @return com.easyboot.framework.core.ActionResult
     **/
    @RequiresPermissions("admin:loginlog:delete")
    @GetMapping("/removeAll")
    public ActionResult removeAll(String ids){
          if(StrUtil.isEmpty(ids)){
              return error("禁止删除");
          }
        loginLogService.removeAll(ids);
        return ok("登录日志已删除");
    }
    /**
     * @author yushuo
     * @description 日志列表
     * @date 16:18 2021/6/25
     * @param [dto]
     * @return com.easyboot.framework.core.ActionResult
     **/
    @RequiresPermissions("admin:loginlog:clear")
    @GetMapping("/clearAll")
    public ActionResult clearAll(){
            loginLogService.clearAll();
            return ok("登录日志已清空");
    }
}
